package camt.andy.demo;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class SubPage extends Activity implements OnItemClickListener {

	ListView functionList;
	private static ArrayList<HashMap<String, String>> mylist;
	SimpleAdapter myAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.subpage);

		functionList = (ListView) findViewById(R.id.lv_Demo_function);

		setupAdapter();

		functionList.setAdapter(myAdapter);
		functionList.setOnItemClickListener(this);
	}

	private void setupAdapter() {
		// TODO Auto-generated method stub

		if (mylist == null) {
			mylist = new ArrayList<HashMap<String, String>>();
			
			HashMap<String, String> mapBass = new HashMap<String, String>();
			mapBass.put("name", "Bass");
			mapBass.put("birthday", "18/12/1986");
			mapBass.put("status", "in relationship");

			mylist.add(mapBass);

			HashMap<String, String> mapJek = new HashMap<String, String>();
			mapJek.put("name", "Jek");
			mapJek.put("birthday", "17/09/1947");
			mapJek.put("status", "in relationship");

			mylist.add(mapJek);

			HashMap<String, String> mapPhongneng = new HashMap<String, String>();
			mapPhongneng.put("name", "Phongneng");
			mapPhongneng.put("birthday", "12/02/1987");
			mapPhongneng.put("status", "in relationship");

			mylist.add(mapPhongneng);
		}
		
		myAdapter = new SimpleAdapter(this, mylist, R.layout.subpagerow,
				new String[] { "name","birthday", "status" },
				new int[] { R.id.tv_Name, R.id.tv_Bday,
						R.id.tv_Status });
		
		functionList.setAdapter(myAdapter);

	}

	public void onItemClick(AdapterView<?> adapter, View arg1, int position, long arg3) {
		// TODO Auto-generated method stub

		Intent startEditPage = new Intent("camt.andy.demo.EDITPAGE");	
		startEditPage.putExtra("name",mylist.get(position).get("name"));
		startEditPage.putExtra("birthday",mylist.get(position).get("birthday"));
		startEditPage.putExtra("status",mylist.get(position).get("status"));
		startEditPage.putExtra("position", position);
		
		startActivityForResult(startEditPage, 1);
		
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		if (requestCode == 1 && resultCode == -1){
			
			String getExName  = data.getExtras().getString("name");
			String getExBirthday = data.getExtras().getString("birthday");
			String getExStatus = data.getExtras().getString("status");
			int getExPosition = data.getExtras().getInt("position");
			
			HashMap<String, String> mapExEdit = new HashMap<String, String>();
			mapExEdit.put("name", getExName);
			mapExEdit.put("birthday", getExBirthday);
			mapExEdit.put("status", getExStatus);
			
			mylist.remove(getExPosition);
			mylist.add(getExPosition, mapExEdit);
			
			myAdapter = new SimpleAdapter(this, mylist, R.layout.subpagerow,
					new String[] { "name","birthday", "status" },
					new int[] { R.id.tv_Name, R.id.tv_Bday,
							R.id.tv_Status });

			functionList.setAdapter(myAdapter);
		}
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		super.onCreateOptionsMenu(menu);
		
		MenuInflater subpageMenu = getMenuInflater();
		subpageMenu.inflate(R.menu.subpagemenu, menu);

		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		super.onOptionsItemSelected(item);
		
		switch (item.getItemId()) {
		case R.id.mn_reset:
	
			mylist = null;
			setupAdapter();
			
			break;
			
		}
		
		return false;
	}
	
	
	
	
	

}
